home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / tcqbsnip.zip / SORT.BAS < prev    next >
BASIC Source File  |  1997-06-20  |  1KB  |  44 lines

  1. ' SORT.BAS
  2. ' by Tika Carr
  3. ' (date unknown)
  4. '
  5. ' Donated to the public domain
  6. ' No warranties or guarantees are expressed or implied.
  7. '
  8. ' Purpose: Two routines to sort numbers or strings. Uses the Bubble Sort
  9. '          algorythm.
  10.  
  11. DECLARE SUB numsort (a%())
  12. DECLARE SUB strsort (a$())
  13.  
  14. DEFINT A-Z
  15.  
  16. 'sort numbers: numsort
  17. DIM a(5)
  18. a(1) = 46: a(2) = 4: a(3) = 100: a(4) = 12: a(5) = 89
  19. PRINT "Unsorted: ": FOR i = 1 TO 5: PRINT a(i), : NEXT: PRINT
  20. numsort a()
  21. PRINT "Sorted: ": FOR i = 1 TO 5: PRINT a(i), : NEXT: PRINT
  22.  
  23. 'sort strings: strsort
  24. REDIM a$(5)
  25. a$(1) = "cat": a$(2) = "antelope": a$(3) = "dog"
  26. a$(4) = "bear": a$(5) = "buffalo"
  27. PRINT "Unsorted: ": FOR i = 1 TO 5: PRINT a$(i), : NEXT: PRINT
  28. strsort a$()
  29. PRINT "Sorted: ": FOR i = 1 TO 5: PRINT a$(i), : NEXT: PRINT
  30. END
  31.  
  32. SUB numsort (a())
  33. FOR c = 1 TO UBOUND(a): FOR d = 1 TO UBOUND(a) - 1
  34.   IF a(d) > a(d + 1) THEN SWAP a(d), a(d + 1)
  35. NEXT d, c
  36. END SUB
  37.  
  38. SUB strsort (a$())
  39. FOR c = 1 TO UBOUND(a$): FOR d = 1 TO UBOUND(a$) - 1
  40.   IF a$(d) > a$(d + 1) THEN SWAP a$(d), a$(d + 1)
  41. NEXT d, c
  42. END SUB
  43.  
  44.